package com.ouheng.shop.controller;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ouheng.common.annotation.Log;
import com.ouheng.common.config.Constant;
import com.ouheng.common.domain.DictDO;
import com.ouheng.common.domain.Tree;
import com.ouheng.common.service.DictService;
import com.ouheng.common.utils.PageUtils;
import com.ouheng.common.utils.Query;
import com.ouheng.common.utils.R;
import com.ouheng.common.utils.ServletsUtils;
import com.ouheng.shop.dao.ProductRelevanceDao;
import com.ouheng.shop.domain.AxImageDO;
import com.ouheng.shop.domain.ProductColorDO;
import com.ouheng.shop.domain.ProductDO;
import com.ouheng.shop.domain.ProductGroupDO;
import com.ouheng.shop.domain.ProductPriceDO;
import com.ouheng.shop.domain.ProductProductGroupDO;
import com.ouheng.shop.domain.ProductPropertyDO;
import com.ouheng.shop.domain.ProductSizeDO;
import com.ouheng.shop.service.AxImageService;
import com.ouheng.shop.service.ProductGroupService;
import com.ouheng.shop.service.ProductProductGroupService;
import com.ouheng.shop.service.ProductRelevanceService;
import com.ouheng.shop.service.ProductService;

/**
 * 
 * 
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2018-08-22 11:18:15
 */
 
@Controller
@RequestMapping("/shop/product")
public class ProductController {
	@Autowired
	private ProductService productService;
	
	@Autowired
	private ProductProductGroupService productProductGroupService;
	
	@Autowired
	private ProductGroupService productGroupService;
	
	@Autowired
	private AxImageService axImageService;
	
	@Autowired
	private DictService dictService;
	
	@Autowired
	private ProductRelevanceService productRelevanceService;
	
	@GetMapping()
	@RequiresPermissions("shop:product:product")
	String Product(){
	    return "shop/product/product";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("shop:product:product")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<ProductDO> productList = productService.list(query);
		int total = productService.count(query);
		PageUtils pageUtils = new PageUtils(productList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("shop:product:add")
	String add(Model model){
		List<DictDO> listDict = dictService.listTypeOrderBySort("product_parameter");
		model.addAttribute("listDict", listDict);
	    return "shop/product/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("shop:product:edit")
	String edit(@PathVariable("id") Long id,Model model){
		ProductDO product = productService.get(id);
		model.addAttribute("product", product);
	    return "shop/product/edit";
	}
	
	@GetMapping("/snatch/{id}")
	@RequiresPermissions("shop:product:edit")
	String snatch(@PathVariable("id") Long id,Model model){
		ProductDO product = productService.get(id);
		model.addAttribute("product", product);
	    return "shop/product/snatch";
	}
	
	/**
	 * 保存
	 */
	@Log("添加商品")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("shop:product:add")
	public R save( ProductDO product,HttpServletRequest request){
		product.setIssnatch("1");
		try {
			List<Object> listproperty = ServletsUtils.convertObjectList(ProductPropertyDO.class, request,"property_");
			List<Object> listprice = ServletsUtils.convertObjectList(ProductPriceDO.class, request,"price_");
			List<Object> listsize = ServletsUtils.convertObjectList(ProductSizeDO.class, request,"size_");
			List<Object> listcolor = ServletsUtils.convertObjectList(ProductColorDO.class, request,"color_");
			if(productService.save(product,listproperty,listprice,listsize,listcolor)>0){
				return R.ok();
			}
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IntrospectionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@Log("编辑商品")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("shop:product:edit")
	public R update( ProductDO product){
		productService.update(product);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@Log("删除商品")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("shop:product:remove")
	public R remove( Long id){
		if(productService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@Log("批量删除商品")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("shop:product:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ids){
		productService.batchRemove(ids);
		return R.ok();
	}
	
	@ResponseBody
	@GetMapping("/listGroup")
	@RequiresPermissions("shop:product:relevance")
	public PageUtils listGroup(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<ProductProductGroupDO> productProductGroupList = productProductGroupService.list(query);
		int total = productProductGroupService.count(query);
		PageUtils pageUtils = new PageUtils(productProductGroupList, total);
		return pageUtils;
	}
	
	@GetMapping("/group/{id}")
	@RequiresPermissions("shop:product:relevance")
	String group(@PathVariable("id") Long id,Model model){
		model.addAttribute("id", id);
	    return "shop/product/productProductGroup";
	}
	
	@GetMapping("/addGroup/{productid}")
	@RequiresPermissions("shop:product:addGroup")
	String addGroup(@PathVariable("productid") Long productid,Model model){
		model.addAttribute("productid", productid);
	    return "shop/product/addProductGroup";
	}

	@GetMapping("/editGroup/{id}")
	@RequiresPermissions("shop:product:editGroup")
	String editGroup(@PathVariable("id") Long id,Model model){
		ProductProductGroupDO productProductGroup = productProductGroupService.get(id);
		model.addAttribute("productProductGroup", productProductGroup);
	    return "shop/product/editProductGroup";
	}
	
	/**
	 * 保存
	 */
	@Log("关联商品分组")
	@ResponseBody
	@PostMapping("/saveGroup")
	@RequiresPermissions("shop:product:addGroup")
	public R saveGroup( ProductProductGroupDO productProductGroup){
		if(productProductGroupService.save(productProductGroup)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@Log("编辑关联商品分组")
	@ResponseBody
	@RequestMapping("/updateGroup")
	@RequiresPermissions("shop:product:editGroup")
	public R updateGroup( ProductProductGroupDO productProductGroup){
		productProductGroupService.update(productProductGroup);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@Log("删除关联商品分组")
	@PostMapping( "/removeGroup")
	@ResponseBody
	@RequiresPermissions("shop:product:removeGroup")
	public R removeGroup( Long id){
		if(productProductGroupService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@Log("批量删除关联商品分组")
	@PostMapping( "/batchRemoveGroup")
	@ResponseBody
	@RequiresPermissions("shop:product:batchRemoveGroup")
	public R batchRemoveGroup(@RequestParam("ids[]") Long[] ids){
		productProductGroupService.batchRemove(ids);
		return R.ok();
	}
	
	@GetMapping("/tree")
	@ResponseBody
	public Tree<ProductGroupDO> tree() {
		Tree<ProductGroupDO> tree = new Tree<ProductGroupDO>();
		tree = productGroupService.getTree();
		return tree;
	}

	@GetMapping("/treeView")
	String treeView() {
		return "shop/product/productGroupTree";
	}
	
	@Log("添加图片")
	@GetMapping("/image/{id}")
	@RequiresPermissions("shop:product:image")
	String image(@PathVariable("id") Long id,Model model){
		ProductDO product = productService.get(id);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("pkid", id);
		map.put("type", 1);
		List<AxImageDO> listAxImageDO = axImageService.list(map);
		model.addAttribute("product", product);
		model.addAttribute("listAxImageDO", listAxImageDO);
		Map<String, Object> map1 = new HashMap<String, Object>();
		map1.put("pkid", id);
		map1.put("type", 3);
		map1.put("sort", "sort");
		map1.put("order","asc");
		List<AxImageDO> listAxImageDO1 = axImageService.list(map1);
		model.addAttribute("listAxImageDO1", listAxImageDO1);
		model.addAttribute("Oss_Url", Constant.Oss_Url);
	    return "shop/product/image";
	}
	
	/**
	 * 
	 * @Title: saveImage   
	 * @Description: TODO(保存图片)   
	 * @param: @param product
	 * @param: @param request
	 * @param: @return      
	 * @return: R      
	 * @throws
	 */
	@Log("保存图片")
	@ResponseBody
	@PostMapping("/saveImage")
	@RequiresPermissions("shop:product:edit")
	public R saveImage( ProductDO product,HttpServletRequest request){
		try {
			List<Object> list = ServletsUtils.convertObjectList(AxImageDO.class, request,"attachs_");
			List<Object> list1 = ServletsUtils.convertObjectList(AxImageDO.class, request,"attachs1_");
			if(productService.saveImage(product, list,list1)>0){
				return R.ok();
			}
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IntrospectionException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return R.error();
	}
	
//	@GetMapping("/relevance/{id}")
//	@RequiresPermissions("shop:product:prorelevance")
//	String relevance(@PathVariable("id") Long id,Model model) {
//		ProductDO product = productService.get(id);
//		model.addAttribute("product", product);
//		return "shop/product/relevance";
//	}
	
	@GetMapping("/protree/{productid}")
	@ResponseBody
	@RequiresPermissions("shop:product:prorelevance")
	Tree<ProductDO> protree(@PathVariable("productid") Long productid) {
		Tree<ProductDO> tree = productService.getTree(productid);
		return tree;
	}
	
//	@Log("保存关联商品")
//	@ResponseBody
//	@PostMapping("/saveRelevance")
//	public R saveRelevance( ProductDO product){
//		productService.saveRelevance(product);
//		return R.ok();
//	}
	
	
	@GetMapping("/relevance/{id}")
	String relevance(@PathVariable("id")Long id,Model model){
		List<Long> ppids = productRelevanceService.listPPidByProductid(id);
		model.addAttribute("ppids", ppids);
		model.addAttribute("pid", id);
	    return "shop/product/relevance";
	}
	
	@ResponseBody
	@GetMapping("/listrelevance")
	public List<ProductDO> listrelevance(@RequestParam Map<String, Object> params){
		//查询列表数据
		List<ProductDO> productList = productService.list(params);
		return productList;
	}
	
	@Log("保存关联商品")
	@ResponseBody
	@PostMapping("/saveRelevance")
	public R saveRelevance( Long pid,@RequestParam("ppids[]")Long[] ppids){
		productService.saveRelevance(pid,ppids);
		return R.ok();
	}
}
